package com.sanyuan.cloud.module.system.controller.admin.nongmo;

import cn.hutool.core.date.DateUtil;
import com.sanyuan.cloud.framework.apilog.core.annotation.ApiAccessLog;
import com.sanyuan.cloud.framework.common.pojo.CommonResult;
import com.sanyuan.cloud.framework.common.pojo.PageParam;
import com.sanyuan.cloud.framework.common.pojo.PageResult;
import com.sanyuan.cloud.framework.common.util.collection.CollectionUtils;
import com.sanyuan.cloud.framework.common.util.object.BeanUtils;
import com.sanyuan.cloud.framework.excel.core.util.ExcelUtils;
import com.sanyuan.cloud.module.system.annotation.UpdateGroup;
import com.sanyuan.cloud.module.system.controller.admin.nongmo.req.PositionAssignReq;
import com.sanyuan.cloud.module.system.controller.admin.nongmo.req.PositionListReq;
import com.sanyuan.cloud.module.system.controller.admin.nongmo.vo.PositionListVO;
import com.sanyuan.cloud.module.system.service.nongmo.SystemPositionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

import static com.sanyuan.cloud.framework.common.pojo.CommonResult.success;
import static com.sanyuan.cloud.module.system.enums.nongmo.CommonEnums.getNameByCode;


/**
 * @Description: 标点管理
 * @Author: wuyuwei
 * @Date: 2025/3/20
 */
@Tag(name = "管理后台 - 标点管理")
@RequestMapping("/system/position")
@RestController
public class SystemPositionController {

    @Resource
    private SystemPositionService systemPositionService;

    /**
     * 分页查询标点列表
     * @param req
     * @return
     */
    @Operation(summary = "分页查询标点列表")
    @ApiAccessLog
    @PostMapping("/getPositionListByPage")
    public CommonResult<PageResult<PositionListVO>> getPositionListByPage(@RequestBody PositionListReq req){
        PageResult<PositionListVO> pageResult = systemPositionService.getPositionListByPage(req);
        return success(pageResult);
    }

    /**
     * 删除标点
     * @param id
     * @return
     */
    @Operation(summary = "删除标点")
    @ApiAccessLog
    @GetMapping("/delete")
    public CommonResult<Boolean> deleteById(@RequestParam("id") Long id){
        return success(systemPositionService.deleteById(id));
    }

    /**
     * 标点指派
     * @param req
     * @return
     */
    @Operation(summary = "标点指派")
    @ApiAccessLog
    @PostMapping("/assign")
    public CommonResult<Boolean> assign(@RequestBody @Validated(UpdateGroup.class) PositionAssignReq req){
        systemPositionService.assign(req);
        return success(true);
    }

    /**
     * 标点数据导出
     * @param response
     * @param req
     * @throws IOException
     */
    @Operation(summary = "标点数据导出")
    @ApiAccessLog
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody PositionListReq req) throws IOException {
        // ① 查询数据
        req.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PositionListVO> list = systemPositionService.getPositionListByPage(req).getList();
        long current = DateUtil.current();
        String filename = "标点数据"+current+".xlsx";
        response.addHeader("Content-Disposition","attachment;filename="+ URLEncoder.encode(filename, StandardCharsets.UTF_8.name()));
        response.setContentType("application/vnd.ms-excel;charset=UTF-8");
        // ② 导出 Excel
        ExcelUtils.write(response, filename, null, PositionListVO.class,
                CollectionUtils.convertList(list, data->{
                    PositionListVO vo = new PositionListVO();
                    BeanUtils.copyProperties(data,vo);
                    vo.setStatusName(getNameByCode(String.valueOf(data.getStatus())));
                    return vo;
                }));
    }
}
